/**
 * Copyright (C), 2020, Y2 T205
 * FileName: MyRabbitConfig
 * Author: Lenovo
 * Date: 2020-05-27 14:50
 * Description:
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */


package com.atguigu.gulimall.order.config;


import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JavaTypeMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.annotation.PostConstruct;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author Lenovo
 * @create 2020-05-27
 * @since 1.0.0
 */
//序列化默认使用jackson
    @Configuration
public class MyRabbitConfig {

      //  @Autowired
    RabbitTemplate rabbitTemplate;
/*
       public MyRabbitConfig(RabbitTemplate rabbitTemplate){
        this.rabbitTemplate =rabbitTemplate;
        initRabbitTemplate();
    }
*/
@Primary
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){
    RabbitTemplate rabbitTemplate =new RabbitTemplate(connectionFactory);
    this.rabbitTemplate=rabbitTemplate;
    rabbitTemplate.setMessageConverter(messageConverter());
    initRabbitTemplate();
   return rabbitTemplate;
}

@Bean
    public MessageConverter messageConverter(){
       return new Jackson2JsonMessageConverter();
    }

    /*定制rabbitTemplate*/
   // @PostConstruct //MyRabbitConfig对象创建万层之后，执行这个方法
    public void initRabbitTemplate(){
//        设置确认回调
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            /*
            * correlationData 当前消息的唯一关联数据（这个是消息的唯一id）
            * ack   消息是否成功收到
            * cause  失败的原因
            * */
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                System.out.println("confirm...correlationData["+correlationData+"]==>ack["+ack+"]==>["+cause+"]");
            }
        });

        //    设置消息抵达队列的确认回调
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
//            只要消息没有投递给指定的队列，就会出法这个失败回调
            /*
            * message 投递失败的详细的详细信息
            * replyCode  回复的状态吗
            * replyText   回复的文本
            * exchange   当时这个消息发送给那个交换机
            * routingKey  当时这个喜喜用那个路由键
            *
            * */
            @Override
            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
                System.out.println("fail Message["+message+"]+replyCode"+replyCode+"replayText"+replyText+"exchange"+exchange+"routingKey"+routingKey);
            }
        });
    }




}
